package com.example.jpatest.controller.restful;

import com.example.jpatest.entity.Competition;
import com.example.jpatest.entity.Enter;
import com.example.jpatest.entity.Team;
import com.example.jpatest.exception.competitionException.CompetitionNotExistException;
import com.example.jpatest.exception.enterException.HasEnterException;
import com.example.jpatest.exception.enterException.NoEnterException;
import com.example.jpatest.exception.teamException.TeamNotExistException;
import com.example.jpatest.service.EnterService;
import com.example.jpatest.service.impl.CompetitionServiceImpl;
import com.example.jpatest.service.impl.EnterServiceImpl;
import com.example.jpatest.service.impl.TeamServiceImpl;
import com.example.jpatest.unit.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class EnterController {

    @Autowired
    TeamServiceImpl teamService;

    @Autowired
    CompetitionServiceImpl competitionService;

    @Autowired
    EnterServiceImpl enterService;

    /**
     * 取消报名
     */
    @DeleteMapping("/enter")
    public Result cancelEnter(@RequestParam(name = "teamName") String teamName,
                              @RequestParam(name = "competitionName") String competitionName){
        Team team = teamService.getByName(teamName);
        Competition competition = competitionService.getCompetitionByName(competitionName);
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (competition == null) {
            throw new CompetitionNotExistException();
        }
        if(enterService.getEnter(team,competition)==null){
            throw new NoEnterException();
        }
        enterService.cancelEnter(team,competition);
        return Result.succMsg();
    }

    /**
     * 进行报名
     */
    @PostMapping("/enter")
    public Result addEnter(@RequestParam(name = "teamName") String teamName,
                           @RequestParam(name = "competitionName") String competitionName){
        Team team = teamService.getByName(teamName);
        Competition competition = competitionService.getCompetitionByName(competitionName);
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (competition == null) {
            throw new CompetitionNotExistException();
        }
        if(enterService.getEnter(team,competition)!=null){
            throw new HasEnterException();
        }
        enterService.enter(team,competition);
        return Result.succMsg();
    }

    /**
     * 获得所有报名
     */
    @GetMapping("/enters")
    public Result getEnters(){
        return Result.succ(enterService.getEnters());
    }

    /**
     * 通过id获得获奖信息
     */
     @GetMapping("/enter/{id}/win")
    public Result getWinInfo(@PathVariable Long id){
         Enter enter = enterService.getById(id);
         if (enter == null) {
             throw new NoEnterException();
         }
         return Result.succ(enterService.getWinInfo(enter));
     }

    /**
     * 查找队伍在竞赛的报名
     */
    @GetMapping("/enter")
    public Result getEnter(@RequestParam(name = "teamName") String teamName,
                           @RequestParam(name = "competitionName") String competitionName){
        Team team = teamService.getByName(teamName);
        Competition competition = competitionService.getCompetitionByName(competitionName);
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (competition == null) {
            throw new CompetitionNotExistException();
        }
        if(enterService.getEnter(team,competition)==null){
            throw new NoEnterException();
        }
        return Result.succ(enterService.getEnter(team,competition));
    }
}
